//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 15
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace DetermineUnacceptableGroupingProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            DetermineUnacceptableGroupingProperties(binding);
        }

        /// <summary>
        /// Determine which FieldURIs are unacceptable for grouping (Listing 15-18)
        /// </summary>
        /// <param name="binding">Binding to use</param>
        ///
        static void DetermineUnacceptableGroupingProperties(
                                    ExchangeServiceBinding binding)
        {
            FindItemType request = new FindItemType();
            // Set up our grouping
            //
            GroupByType grouping = new GroupByType();

            grouping.AggregateOn = new AggregateOnType();
            grouping.AggregateOn.Item = new
            PathToUnindexedFieldType(UnindexedFieldURIType.itemDateTimeCreated);
            grouping.AggregateOn.Aggregate = AggregateType.Maximum;
            grouping.Order = SortDirectionType.Ascending;
            request.Item1 = grouping;
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
            request.Traversal = ItemQueryTraversalType.Shallow;
            DistinguishedFolderIdType drafts = new DistinguishedFolderIdType();
            drafts.Id = DistinguishedFolderIdNameType.drafts;
            request.ParentFolderIds = new BaseFolderIdType[] { drafts };
            // Create a dictionary to hold all the invalid URIs. Map FieldURI to
            // error code.
            //
            Dictionary<UnindexedFieldURIType, ResponseCodeType> invalidURIs = new
            Dictionary<UnindexedFieldURIType, ResponseCodeType>();
            // Grab all of the FieldURIs...
            //
            Array fieldURIValues = Enum.GetValues(typeof(UnindexedFieldURIType));
            // Iterate across each field URI and make the call. If we get back an
            // error, add it to the dictionary
            //
            foreach (object value in fieldURIValues)
            {
                int fieldURIInt = (int)value;
                grouping.Item = new
                PathToUnindexedFieldType((UnindexedFieldURIType)fieldURIInt);
                FindItemResponseType response = binding.FindItem(request);
                FindItemResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as FindItemResponseMessageType;
                if (responseMessage.ResponseCode != ResponseCodeType.NoError)
                {
                    invalidURIs.Add(
                    (UnindexedFieldURIType)fieldURIInt,
                    responseMessage.ResponseCode);
                }
            }
            // now dump out the bad ones....
            using (StreamWriter writer =
                    File.CreateText(@"c:\unsupportedURIsForGrouping.txt"))
            {
                writer.WriteLine("Unsupported FieldURIs for grouping");
                writer.WriteLine("=====================================");
                foreach (KeyValuePair<UnindexedFieldURIType, ResponseCodeType> pair
                in invalidURIs)
                {
                    writer.WriteLine("URI: {0}, Error: {1}", pair.Key, pair.Value);
                }
                writer.Flush();
            }
        }
    }
}
